package com.example.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created on 2019-07-18
 */
@RequestMapping("/product/")
@RestController
public class ProductController {
    @RequestMapping("list")
    public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response) {

        Cookie[] cookies = request.getCookies();
        final Map<String, Object> cookieMap = new HashMap<String, Object>();


        if (cookies != null) {
            Arrays.stream(cookies)
                    .forEach(e -> cookieMap.put(e.getName(), e.getValue()));
        }


        Map<String, Object> returnMap = new HashMap<>();
//        String queryString = request.getQueryString();


//        returnMap.put("queryString", queryString);
        returnMap.put("cookies", cookieMap);
        returnMap.put("headers", getHeadersInfo(request));
        returnMap.put("parameter", getParamMap(request));


        return returnMap;
    }


    @PostMapping("upload")
    public Map<String, Object> upload(HttpServletRequest request,
            @RequestParam(value = "files") List<MultipartFile> files) {
        Cookie[] cookies = request.getCookies();
        final Map<String, Object> cookieMap = new HashMap<String, Object>();


        Arrays.stream(cookies).forEach(e -> cookieMap.put(e.getName(), e.getValue()));


        Map<String, Object> returnMap = new HashMap<>();
//        String queryString = request.getQueryString();


//        returnMap.put("queryString", queryString);
        returnMap.put("cookies", cookieMap);
        returnMap.put("headers", getHeadersInfo(request));
        returnMap.put("parameter", getParamMap(request));


        List<Map<String, Object>> list = new ArrayList<>();
        files.forEach(file -> {
            Map<String, Object> fileMap = new HashMap<>();
            fileMap.put("filename", file.getName());
            fileMap.put("contentType", file.getContentType());
            fileMap.put("originalFilename", file.getOriginalFilename());
            fileMap.put("size", file.getSize());

            list.add(fileMap);
        });

        returnMap.put("files", list);


        return returnMap;
    }


    private Map<String, Object> getParamMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        Enumeration paramNames = request.getParameterNames();
        if (paramNames != null) {
            while (paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();

                String[] paramValues = request.getParameterValues(paramName);
                if (paramValues == null) {
                    map.put(paramName, "");
                } else {
                    if (paramValues.length == 1) {
                        map.put(paramName, paramValues[0]);
                    } else {
                        map.put(paramName, paramValues);
                    }
                }
            }
        }

        return map;
    }

    private Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
        }
        return map;
    }

}
